home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / cli / 0Utils13.lha / 0Utils / ReadLn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-20  |  3.4 KB  |  151 lines

  1.  
  2. /******************************************************************************
  3.  
  4.     MODULE
  5.     ReadLn.c
  6.  
  7.     DESCRIPTION
  8.     read one (or more) line(s) from a specified _filehandle_
  9.  
  10.     NOTES
  11.     Kickstart 2.0+ required
  12.     compiles w/ SAS/C v6.51
  13.  
  14.     BUGS
  15.     none known
  16.  
  17.     TODO
  18.  
  19.     EXAMPLES
  20.     > set fh `Open Sys:S/Shell-startup`
  21.     > Readln $fh
  22.       alias xcopy "copy clone "
  23.     > Close $fh
  24.  
  25.     SEE ALSO
  26.  
  27.     INDEX
  28.  
  29.     HISTORY
  30.     21-02-95 b_noll created
  31.     21-02-95 b_noll added version/format-prefix/offset
  32.     20-03-95 b_noll added args diagnostics
  33.     20-03-95 b_noll ReadLn always skipped one line
  34.  
  35.     AUTHOR
  36.     Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
  37.     b_noll@informatik.uni-kl.de
  38.  
  39. ******************************************************************************/
  40.  
  41. /**************************************
  42.         Includes
  43. **************************************/
  44.  
  45. #ifndef   EXEC_LIBRARIES_H
  46. # include <exec/libraries.h>
  47. #endif /* EXEC_LIBRARIES_H */
  48.  
  49. #ifndef   CLIB_EXEC_PROTOS_H
  50. # include <clib/exec_protos.h>
  51. #endif /* CLIB_EXEC_PROTOS_H */
  52.  
  53. #ifndef   DOS_DOS_H
  54. # include <dos/dos.h>
  55. #endif /* DOS_DOS_H */
  56.  
  57. #ifndef   CLIB_DOS_PROTOS_H
  58. # include <clib/dos_protos.h>
  59. #endif /* CLIB_DOS_PROTOS_H */
  60.  
  61. #include <proto/dos.h>
  62. #include <proto/exec.h>
  63.  
  64. /**************************************
  65.      Defines & Structures
  66. **************************************/
  67.  
  68. #ifndef ABSEXECBASE
  69. #define ABSEXECBASE ((struct ExecBase **)4L)
  70. #endif
  71.  
  72. struct _arg {
  73. /* ******************** USER FORMAT ******************** */
  74. #define FORMAT "FILEHANDLE/N/A,LINES/N/K"
  75.  
  76.     BPTR  *filehandle;
  77.     ULONG *lines;
  78.  
  79. /* ******************** USER FORMAT ******************** */
  80. }; /* struct _argv */
  81.  
  82. #define MAXPATHLEN 256
  83. #define MAXLINELEN 256
  84.  
  85. #define VERSIONPREFIX    "\0$VER: "
  86. #define VERSIONOFFSET    0
  87. #define FORMATPREFIX    "\0$ARG: "
  88. #define FORMATOFFSET    7
  89.  
  90. /**************************************
  91.         Implementation
  92. **************************************/
  93.  
  94. long _main (void)
  95. {
  96.     const char* version = VERSIONPREFIX "ReadLn 1.1 " __AMIGADATE__  + VERSIONOFFSET;
  97.     long retval = RETURN_FAIL;
  98.     struct ExecBase*SysBase = *ABSEXECBASE;
  99.     struct Library* DOSBase;
  100.  
  101.     if (DOSBase = OpenLibrary (DOSNAME, 37)) {
  102.     struct _arg argv = { 0 };
  103.     APTR   args;
  104.     retval     = RETURN_ERROR;
  105.     if (args = (void*)ReadArgs(FORMATPREFIX FORMAT + FORMATOFFSET, (LONG*)&argv, NULL)) {
  106.  
  107. /* ******************** USER BODY ******************** */
  108.  
  109.         if (argv.filehandle && *argv.filehandle) {
  110.         UBYTE buffer[MAXLINELEN];
  111.         ULONG lines = 1;
  112.  
  113.         /* MISSING: CHECKREGISTER(*argv.filehandle); */
  114.  
  115.         if (argv.lines) lines = *argv.lines;
  116.         retval = RETURN_OK;
  117.  
  118.         while ((retval == RETURN_OK) && lines && FGets(*argv.filehandle, buffer, MAXLINELEN)) {
  119.             -- lines;
  120.             if (PutStr(buffer)) {
  121.             retval = RETURN_ERROR;
  122.             //PrintFault(IoErr(), "Read");
  123.             } /* if */
  124.         } /* while */
  125.  
  126.         if (lines && !retval) {
  127.             //PrintFault(IoErr(), "Read");
  128.             retval = RETURN_WARN;
  129.         } /* if */
  130.         } else {
  131.         //PrintFault(ERROR_BAD_NUMBER, "Read");
  132.         SetIoErr(ERROR_BAD_NUMBER);
  133.         } /* if */
  134.  
  135. /* ******************** USER BODY ******************** */
  136.         FreeArgs (args);
  137.     } /* if */
  138.  
  139.     if (retval > RETURN_WARN)
  140.         PrintFault(IoErr(), "Read");
  141.  
  142.     CloseLibrary (DOSBase);
  143.     } /* if */
  144.     return (retval);
  145. } /* _main */
  146.  
  147. /******************************************************************************
  148. *****  END ReadLn.c
  149. ******************************************************************************/
  150.  
  151.